<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">

          <el-col :lg="6" :md="12">
            <el-form-item label="名称:">
              <el-input
                clearable
                v-model="where.title"
                placeholder="请输入名称"/>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:index:add')">添加
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            v-if="permission.includes('sys:index:dall')">删除
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="permission.includes('sys:index:edit')">修改
          </el-link>

          <el-popconfirm
            class="ele-action"
            title="确定要删除此商家优惠券吗？"
            @confirm="remove(row)">
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:index:delete')">删除
            </el-link>
          </el-popconfirm>
        </template>
        <template slot="status" slot-scope="{row}">
          <el-switch
            v-model="row.status"
            @change="status(row)"
            :active-value="1"
            :inactive-value="0"/>
        </template>
        <template slot="is_status" slot-scope="{row}">
          <el-switch
            v-model="row.is_status"
            @change="tanstatus(row)"
            :active-value="1"
            :inactive-value="0"/>
        </template>

        <template slot="is_status" slot-scope="{row}">
          <el-tag v-if="row.is_status === 1" type="success" size="small">已开启</el-tag>
          <el-tag v-if="row.is_status === 2" type="danger" size="small">未开启</el-tag>
        </template>

        <template slot="preference" slot-scope="{row}">
          <el-tag v-if="row.preference === 1" type="success" size="small">满减优惠</el-tag>
          <el-tag v-if="row.preference === 2" type="danger" size="small">折扣优惠</el-tag>
        </template>
        <!-- 突变地址列 -->
        <template slot="image_url" slot-scope="{row}">
          <el-avatar shape="square" :size="25" :src="row.image_url"/>
        </template>


      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <coupon-edit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"/>
    <coupon-area
      :data="current"
      :visible.sync="showArea"/>
  </div>
</template>

<script>
import {mapGetters} from "vuex";
import CouponEdit from './coupon-edit';
import CouponArea from './coupon-area';

export default {
  name: 'ExamCoupon',
  components: {CouponEdit, CouponArea},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/pricecoupon/index',
      // 表格列配置
      columns: [


        {
          prop: 'title',
          label: '名称',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center',

        },
        {
          columnKey: 'action',
          label: '操作',
          width: 200,
          align: 'center',
          resizable: false,
          slot: 'action',

        },
        {
          columnKey: 'image_url',
          label: '图片地址',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
          slot: 'image_url'
        },

     {
          prop: 'hours_add_time',
          label: '开始时间段',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },


        {
          prop: 'hours_end_time',
          label: '结束期限段',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },

 {
          prop: 'shichang',
          label: '时长',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },

        {
          prop: 'price',
          label: '优惠价格',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'num',
          label: '数量',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },

     {
          prop: 'receive_num',
          label: '已售数量',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },



        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,

        },
        {
          columnKey: 'action',
          label: '操作',
          width: 200,
          align: 'center',
          resizable: false,
          slot: 'action',

        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      showArea: false,
    };
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 显示设置区域 */
    openArea(row) {
      this.current = row;
      this.showArea = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/pricecoupon/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 更改状态 */
    status(row) {
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "id": row.id,
        "status": row.status
      })
      this.$http.post('/pricecoupon/status', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
        } else {
          row.status = !row.status ? 1 : 2;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的商家优惠券吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/pricecoupon/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
  }
}
</script>

<style scoped>
</style>
